<%@page import="java.sql.Statement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%
     String data = "";
       String sql = "";
       String json = "";
       int errorcode = 0;
       ResultSet res = null;
       model.ConnectionDB connection = new model.ConnectionDB();

       /* if(request.getParameter("Action").equals("Add")){
            sql = "SELECT Addvehicles(" + request.getParameter("MODEL") +  ",'" + request.getParameter("PLATE") + "'," + request.getParameter("CAPACITY") + "," + request.getParameter("NAMEPRIN") +"," + request.getParameter("NAMERE")+",'" + request.getParameter("TYPE")+"')";
                try {
                            connection.connect();
                            res=connection.consult(sql);
                            connection.getCon().close();
                            if(res.next())
                                data="{'ans' : " + res.getInt(1) + "}";//Add
                    } catch (Exception e) {
                            if(res.next())
                                data="{'ans' : " + res.getInt(1) + "}";//Add
                    } catch (Exception e) {
                            errorcode = -7;
                            connection.getCon().close();
                    }
            }
        else if(request.getParameter("Action").equals("Update")){
            sql = "SELECT UpdateVehicles(" + request.getParameter("MODEL") +  ",'" + request.getParameter("PLATE") + "'," + request.getParameter("CAPACITY") + "," + request.getParameter("NAMEPRIN") +"," + request.getParameter("NAMERE")+",'" + request.getParameter("TYPE")+"')";
               try {
                            connection.connect();
                            res = connection.consult(sql);
                            connection.getCon().close();
                            if(res.next())
                                data="{'ans' : " + res.getInt(1) + "}";//update
                    } catch (Exception e) {
                            out.print(e.getMessage());
                             errorcode = -8;
                  }
            }

        else if(request.getParameter("Action").equals("Delete")){
            sql = "SELECT DeleteVehicles(" + request.getParameter("PLATE")+ ")";

                try {
                            connection.connect();
                            connection.consult(sql);
                            connection.getCon().close();
                    } catch (Exception e) {
                            out.print(e.getMessage());
                                errorcode = -1;
                                connection.getCon().close();
                  }
            }

       else */if(request.getParameter("Action").equals("Drive1")){
            sql = "SELECT * FROM seekdrivers();";
            //out.print(sql);
            try {
                connection.connect();
                res = connection.consult(sql);
                connection.getCon().close();

                while (res.next()) {
            //if(res.next()){
            //HttpSession sessionUser=request.getSession();//Create new Session
            //sessionUser.setAttribute("result",res);
                    if (!data.equals("")) {
                        data += ",";
                    }
                    data += '{';
                    data += "'i' :" + res.getInt(1);
                    //data += "'state' :'" + res.getString("city_state") + "', ";
                    //data += "'idCity': " + res.getInt("city_id") + ", ";
                    data += '}';
                }
            }catch(Exception e) {
                out.print(e.getMessage());
                out.print("{'errorcode' : -1}"); //
                connection.getCon().close();
            }
           /* else  if(request.getParameter("Action").equals("Drive2")){
            sql = "SELECT * FROM seekdrivers();";
            //out.print(sql);
            try {
                connection.connect();
                res = connection.consult(sql);
                connection.getCon().close();

                while (res.next()) {
            //if(res.next()){
            //HttpSession sessionUser=request.getSession();//Create new Session
            //sessionUser.setAttribute("result",res);
                    if (!data.equals("")) {
                        data += ",";
                    }
                    data += '{';
                    data += "'n' :" + res.getInt(1);
                    //data += "'state' :'" + res.getString("city_state") + "', ";
                    //data += "'idCity': " + res.getInt("city_id") + ", ";
                    data += '}';
                }
            }catch(Exception e) {
                out.print(e.getMessage());
                out.print("{'errorcode' : -1}"); //
                connection.getCon().close();
            }
            }
            */

        json = "{'errorcode' : " + errorcode + ", 'data' : [" + data + "]}";
       out.print(json);
%>